package cn.aylog.springboot.controller;


import cn.aylog.common.entity.R;
import cn.aylog.springboot.entity.Course;
import cn.aylog.springboot.entity.CourseDescription;
import cn.aylog.springboot.entity.vo.CourseVo;
import cn.aylog.springboot.service.ICourseDescriptionService;
import cn.aylog.springboot.service.ICourseService;
import cn.aylog.springboot.service.ICourseVoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author YongTay
 * @since 2022-03-28
 */
@Api("课程管理模块")
@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private ICourseVoService courseVoService;

    @ApiOperation("添加课程")
    @PostMapping("/add")
    public R add(@RequestBody CourseVo vo) {
        String courseId = courseVoService.addCourseVo(vo);
        if(StringUtils.hasLength(courseId)) {
            return R.ok().message("添加成功").data("id", courseId);
        }
        return R.error().message("添加失败");
    }

    @ApiOperation("根据id查询课程")
    @GetMapping("/{courseId}")
    private R queryById(@PathVariable String courseId) {
        List<CourseVo> vos = courseVoService.queryByIds(Arrays.asList(courseId));
        if(vos.isEmpty()) {
            return R.ok();
        }
        return R.ok().data("data", vos.get(0));
    }

    @ApiOperation("根据id更新课程信息")
    @PostMapping("/update")
    public R update(@RequestBody CourseVo vo) {
        boolean success = courseVoService.updateCourseVo(vo);
        if(!success) {
            return R.error().message("更新失败");
        }
        return R.ok().message("更新成功");
    }

}
